package test.nettys.nio;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author wangshaopeng
 * @Date 2021/8/11
 */
public class ServerClient {

    public static void main(String[] args) throws IOException {


        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);

        Selector selector = Selector.open();

        socketChannel.register(selector, SelectionKey.OP_CONNECT);

        socketChannel.connect(new InetSocketAddress("127.0.0.1", 8899));


        while (true){
            selector.select();
            Set<SelectionKey> selectionKeys = selector.selectedKeys();

            for (SelectionKey selectionKey : selectionKeys) {
                if (selectionKey.isConnectable()) {
                    SocketChannel client = (SocketChannel) selectionKey.channel();
                    String name = String.valueOf(client.getRemoteAddress());

                    if (client.isConnectionPending()) {
                        client.finishConnect();
                        ByteBuffer write = ByteBuffer.allocate(100);

                        write.put((name + "链接成功").getBytes());
                        write.flip();
                        client.write(write);

                        ExecutorService service = Executors.newSingleThreadScheduledExecutor(Executors.defaultThreadFactory());
                        service.execute(()->{
                            while (true){
                                write.clear();
                                InputStreamReader reader = new InputStreamReader(System.in);
                                BufferedReader br = new BufferedReader(reader);
                                String msg = null;
                                try {
                                    msg = br.readLine();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                System.out.println("nia a ");
                                write.put(msg.getBytes());
                                write.flip();
                                try {
                                    client.write(write);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }

                        });


                    }
                    client.register(selector, SelectionKey.OP_READ);
                }else if(selectionKey.isReadable()){
                    SocketChannel client = (SocketChannel) selectionKey.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(100);
                    int read = client.read(buffer);
                    if (read>0) {
                        System.out.println("收到:"+new String(buffer.array(), 0, read));
                    }

                }
            }
            selectionKeys.clear();
        }




    }



}
